﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibNiconico.NiconicoApi.Internal;
using LibNiconico.NiconicoApi.Lives.Comments.PostKey;
using LibNiconico.NiconicoApi.Lives.WatchPage;
using LibNiconico.NiconicoApi.PlayerStatus;

namespace LibNiconico.NiconicoApi.Lives.Comments
{
    /// <summary>
    /// ニコニコ生放送コメント接続情報
    /// </summary>
    public sealed class NicoLiveCommentConnectionInfo:NicoXmlSocketCommentConnectionInfo
    {
        /// <summary>
        /// 放送主か判定の取得
        /// </summary>
        public bool IsOwner { get; internal set; }
        
        /// <summary>
        /// 接続先の放送を行っているコミュニティの取得
        /// </summary>
        public string DefaultCommunity { get; internal set; }

        /// <summary>
        /// 生放送ID
        /// </summary>
        public string Id { get; internal set; }
        
        /// <summary>
        /// 接続先の生放送番号の取得
        /// </summary>
        public string LiveId { get; internal set; }
        
        /// <summary>
        /// 生放送接続履歴の取得・設定
        /// </summary>
        public NicoLiveHistory History { get; set; }
        
        /// <summary>
        /// 接続したユーザがプレミアムかの判定を取得
        /// </summary>
        public bool IsPremium { get;internal set; }

        /// <summary>
        /// コンストラクタ
        /// </summary>
        private NicoLiveCommentConnectionInfo()
            : base()
        {
            IsOwner = false;
            DefaultCommunity = "";

            History = null;
        }

        /// <summary>
        /// プレイヤーステータスからコメント接続情報を生成
        /// </summary>
        /// <param name="playerStatus"></param>
        /// <param name="history"></param>
        /// <returns></returns>
        internal static NicoLiveCommentConnectionInfo Create(NicoPlayerStatus playerStatus,NicoLiveHistory history)
        {
            var connetInfo= new NicoLiveCommentConnectionInfo
            {
                DefaultCommunity = playerStatus.Stream.DefaultCommunity,
                Host = playerStatus.Ms.Addr,
                Id=playerStatus.Stream.Id,
                IsOwner = playerStatus.Stream.IsOwner,
                Port = playerStatus.Ms.Port,
                Thread = playerStatus.Ms.Thread.ToString(),
                OpenTime = playerStatus.Stream.OpenTime,
                EndTime = playerStatus.Stream.EndTime,
                UserDomain = playerStatus.User.UserDomain,
                UserId = playerStatus.User.UserId,
                Locale = playerStatus.User.UserDomain,
                Langage = playerStatus.User.UserLanguage,
                LiveId = playerStatus.Stream.Id,
                IsPremium = playerStatus.User.IsPremium,

            };

            return connetInfo;
        }
    }
}
